package com.cloud.utils;

import lombok.SneakyThrows;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;

public class CsvUtil {

    public static void main(String[] arges){
        //自定义数组
        ArrayList<ArrayList<String>> alldata=new ArrayList<ArrayList<String>>();
        alldata.add(new ArrayList<String>(Arrays.asList("统一社会信用代码","单位名称","行业代码","入规标准")));  //添加一行
//        alldata.add(new ArrayList<String>(Arrays.asList("ID","ANY","md5_phone")));  //添加一行

        ArrayList<ArrayList<String>> data0=new ArrayList<ArrayList<String>>();
        for (int i=0;i<10000000;i++) {
            data0.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
        }
        //保存成csv文件
        Array2CSV(alldata,"f://test9000w.csv");

        //自定义数组
        ArrayList<ArrayList<String>> data=new ArrayList<ArrayList<String>>();
        for (int i=10000000;i<20000000;i++) {
            data.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
        }
        //保存成csv文件
        Array2CSV(data,"f://test9000w.csv");

//        //自定义数组
//        ArrayList<ArrayList<String>> data1=new ArrayList<ArrayList<String>>();
//        for (int i=20000000;i<30000000;i++) {
//            data1.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
//        }
//        //保存成csv文件
//        Array2CSV(data1,"test9000w.csv");
//
//        //自定义数组
//        ArrayList<ArrayList<String>> data2=new ArrayList<ArrayList<String>>();
//        for (int i=30000000;i<40000000;i++) {
//            data2.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
//        }
//        //保存成csv文件
//        Array2CSV(data2,"test9000w.csv");
//
//        //自定义数组
//        ArrayList<ArrayList<String>> data3=new ArrayList<ArrayList<String>>();
//        for (int i=40000000;i<50000000;i++) {
//            data3.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
//        }
//        //保存成csv文件
//        Array2CSV(data3,"test9000w.csv");
//
//        //自定义数组
//        ArrayList<ArrayList<String>> data4=new ArrayList<ArrayList<String>>();
//        for (int i=50000000;i<60000000;i++) {
//            data4.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
//        }
//        //保存成csv文件
//        Array2CSV(data4,"test9000w.csv");
//
//        //自定义数组
//        ArrayList<ArrayList<String>> data5=new ArrayList<ArrayList<String>>();
//        for (int i=60000000;i<70000000;i++) {
//            data5.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
//        }
//        //保存成csv文件
//        Array2CSV(data5,"test9000w.csv");
//
//        //自定义数组
//        ArrayList<ArrayList<String>> data6=new ArrayList<ArrayList<String>>();
//        for (int i=70000000;i<80000000;i++) {
//            data6.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
//        }
//        //保存成csv文件
//        Array2CSV(data6,"test9000w.csv");
//
//        //自定义数组
//        ArrayList<ArrayList<String>> data7=new ArrayList<ArrayList<String>>();
//        for (int i=80000000;i<90000000;i++) {
//            data7.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
//        }
//        //保存成csv文件
//        Array2CSV(data7,"test9000w.csv");
//
//        //自定义数组
//        ArrayList<ArrayList<String>> data8=new ArrayList<ArrayList<String>>();
//        for (int i=90000000;i<100000000;i++) {
//            data8.add(new ArrayList<String>(Arrays.asList("91220424721046834J"+i,"嘉兴德邦生物科技有限公司"+i,"1329"+i,"2000")));  //添加一行
//        }
//        //保存成csv文件
//        Array2CSV(data8,"test9000w.csv");
    }

    //导出到csv文件
    @SneakyThrows
    public static void Array2CSV(ArrayList<ArrayList<String>> data, String path)
    {
        try (FileOutputStream fileOutputStream = new FileOutputStream(path, true);
             OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK");
             BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter)) {
            for (int i = 0; i < data.size(); i++) {
                ArrayList<String> onerow = data.get(i);
                for (int j = 0; j < onerow.size(); j++) {
//                    bufferedWriter.write(DelQuota(onerow.get(j)));
                    bufferedWriter.write(onerow.get(j));
                    bufferedWriter.write(",");
                }
                bufferedWriter.newLine();
            }


//        try {
//            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"UTF-8"));
//            for (int i = 0; i < data.size(); i++)
//            {
//                ArrayList<String> onerow=data.get(i);
//                for (int j = 0; j < onerow.size(); j++)
//                {
//                    out.write(DelQuota(onerow.get(j)));
//                    out.write(",");
//                }
//                out.newLine();
//            }
//            out.flush();
//            out.close();
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        }
    }
    public static String DelQuota(String str)
    {
        String result = str;
        String[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", ".", "/", ":", "/,", "<", ">", "?" };
        for (int i = 0; i < strQuota.length; i++)
        {
            if (result.indexOf(strQuota[i]) > -1)
                result = result.replace(strQuota[i], "");
        }
        return result;
    }


}
